
/*
 * Copyright (C) 2008-2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: AbstractSchemaUpdate.java 377 2010-03-11 20:50:18Z archie.cobbs $
 */

package org.dellroad.sidekar.schema.change;

import org.dellroad.sidekar.Sidekar;
import org.dellroad.sidekar.schema.model.SchemaModel;

/**
 * {@link SchemaChange} that renames an entity.
 */
public class RenameEntityChange extends EntityChange {

    private final String newEntityName;

    /**
     * Constructor.
     *
     * @param oldName old name of the entity
     * @param newName new name of the entity
     * @throws IllegalArgumentException if either name is not a legal entity name
     */
    public RenameEntityChange(String oldName, String newName) {
        super(oldName);
        Sidekar.checkValidEntityName(newName);
        this.newEntityName = newName;
    }

    public String getNewEntityName() {
        return this.newEntityName;
    }

    @Override
    public String getDescription() {
        return "rename entity `" + this.getEntityName() + "' to `" + this.newEntityName + "'";
    }

    @Override
    public void checkValidFor(SchemaModel schemaModel) {
        super.checkValidFor(schemaModel);
        this.checkEntityExists(schemaModel, this.getEntityName());
        this.checkValidEntityName(this.newEntityName);
        this.checkEntityNotExists(schemaModel, this.newEntityName);
    }

    @Override
    public final void visit(SchemaChangeSwitch schemaChangeSwitch) {
        schemaChangeSwitch.caseRenameEntityChange(this);
    }
}

